package io.renren.modules.pms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.modules.pms.entity.dto.ProductConditionDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.pms.dao.ProductDao;
import io.renren.modules.pms.entity.ProductEntity;
import io.renren.modules.pms.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                new QueryWrapper<ProductEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean updateDeleteStatus(Long[] ids, Integer deleteStatus) {
        List<ProductEntity> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++){
            ProductEntity productEntity = new ProductEntity();
            productEntity.setId(ids[i]);
            productEntity.setDeleteStatus(deleteStatus);
            list.add(productEntity);
        }
        this.updateBatchById(list);
        return true;
    }

    @Override
    public Page list(ProductConditionDTO condition) {
        Page page = new Page(condition.getPage(),condition.getLimit());
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<ProductEntity> lambda = queryWrapper.lambda();
        //商品名称、关键字
        if (!StrUtil.isBlank(condition.getKeyword())) {
            lambda.like(ProductEntity::getKeywords, condition.getKeyword());
            lambda.or().like(ProductEntity::getName, condition.getKeyword());
        }
        //商品货号
        if (!StrUtil.isBlank(condition.getProductSn())){
            lambda.eq(ProductEntity::getProductSn,condition.getProductSn());
        }
        //商品分类
        if (condition.getProductCategoryId() != null && condition.getProductCategoryId() > 0){
            lambda.eq(ProductEntity::getProductCategoryId,condition.getProductCategoryId());
        }
        //商品品牌
        if (condition.getBrandId() != null && condition.getBrandId() > 0){
            lambda.eq(ProductEntity::getBrandId,condition.getBrandId());
        }
        //上架状态
        if (condition.getPublishStatus() != null){
            lambda.eq(ProductEntity::getPublishStatus,condition.getPublishStatus());
        }
        //审核状态
        if (condition.getVerifyStatus() != null){
            lambda.eq(ProductEntity::getVerifyStatus,condition.getVerifyStatus());
        }
        return this.page(page, lambda);
    }

    @Override
    public boolean updatePublishStatus(Long[] ids, Integer publishStatus) {
        List<ProductEntity> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++){
            ProductEntity productEntity = new ProductEntity();
            productEntity.setId(ids[i]);
            productEntity.setPublishStatus(publishStatus);
            list.add(productEntity);
        }
        this.updateBatchById(list);
        return true;
    }

    @Override
    public boolean updateRecommendStatus(Long[] ids, Integer recommendStatus) {
        List<ProductEntity> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++){
            ProductEntity productEntity = new ProductEntity();
            productEntity.setId(ids[i]);
            productEntity.setRecommandStatus(recommendStatus);
            list.add(productEntity);
        }
        this.updateBatchById(list);
        return true;
    }

    @Override
    public boolean updateNewStatus(Long[] ids, Integer newStatus) {
        List<ProductEntity> list = new ArrayList<>();
        for (int i = 0; i < ids.length; i++){
            ProductEntity productEntity = new ProductEntity();
            productEntity.setId(ids[i]);
            productEntity.setNewStatus(newStatus);
            list.add(productEntity);
        }
        this.updateBatchById(list);
        return true;
    }

}
